#include "cocos2d.h"
#include "AppDelegate.h"
#include "SimpleAudioEngine.h"
#include "script_support/CCScriptSupport.h"
#include "CCLuaEngine.h"
#include "GameObject.h"
#include "LuaCCTableView.h"

USING_NS_CC;
using namespace CocosDenshion;

AppDelegate::AppDelegate()
{
    // fixed me
    //_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
}

AppDelegate::~AppDelegate()
{
    // end simple audio engine here, or it may crashed on win32
    SimpleAudioEngine::sharedEngine()->end();
    //CCScriptEngineManager::purgeSharedManager();
}

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
  
  
  CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
  //CCSize designSize = CCSizeMake(480, 320);
  CCSize designSize = CCSizeMake(960, 640);

  /*
  if (screenSize.height > 320)
  {
    //CCFileUtils::sharedFileUtils()->setResourceDirectory("hd");
    pDirector->setContentScaleFactor(640.0f/designSize.height);
  }
  else
  {
    //CCFileUtils::sharedFileUtils()->setResourceDirectory("sd");
    pDirector->setContentScaleFactor(320.0f/designSize.height);
  }*/
  //pDirector->setContentScaleFactor(screenSize.height/designSize.height);
  pDirector->setContentScaleFactor(1);
  CCLog("screen %f %f", screenSize.width, screenSize.height);
  CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionShowAll);

  /*
  CCArray *arr = CCArray::create();
  for(int i=0; i<10; i++){
    CCSprite *sprite = CCSprite::create("Icon.png");
    arr->addObject(sprite);
    sprite->retain();
  }
  MyCCTableView *tv = MyCCTableView::create(kCCScrollViewDirectionVertical, CCSizeMake(60, 250), arr);
  CCScene *sc = CCScene::create();
  sc->addChild(tv);
  tv->setPosition(ccp(240, 160));
  //tv->reloadData();
  
  CCSprite *sp = CCSprite::create("Icon.png");
  sc->addChild(sp);
  pDirector->runWithScene(sc);
  
  return true;
  */
  /*
  GameObject *go = GameObject::create();
  go->addCompant("normal", "hero58.plist", 1, NULL);
  //go->addAction("attack", "hero58.plist");
  CCScene *sc = CCScene::create();
  sc->addChild(go);
  go->setPosition(ccp(240, 160));
  pDirector->runWithScene(sc);
  //go->playAction("normal", "attack", "onAttack:attack");
  return true;*/
    // turn on display FPS
    //pDirector->setDisplayStats(true);
  
    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // register lua engine
    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    CCString* pstrFileContent = CCString::createWithContentsOfFile("hello.lua");
    if (pstrFileContent)
    {
        pEngine->executeString(pstrFileContent->getCString());
    }
#else
    std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("main.lua");
    pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
    pEngine->executeScriptFile(path.c_str());
#endif 
  
  /*
  CCScene *sc = pDirector->m_pNextScene;
  CCArray *arr = sc->getChildren();
  //sc->getChildByTag(<#int tag#>)
  CCLog("--%p", arr->objectAtIndex(0));
  CCLog("----%p", ((CCNode*)arr->objectAtIndex(0))->getChildByTag(11));

  arr = ((CCNode*)arr->objectAtIndex(0))->getChildByTag(11)->getChildren();
  CCLog("------%p", arr->objectAtIndex(0));

  arr = ((CCNode*)arr->objectAtIndex(0))->getChildren();
  for (int i=0; i<arr->count(); i++) {
    CCLog("--------child %p", arr->objectAtIndex(i));
  }*/
    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
    CCDirector::sharedDirector()->stopAnimation();
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
    CCDirector::sharedDirector()->startAnimation();
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
